package it.traveldream.entities;

import it.traveldreamClient.beans.dto.ComponenteDTO;
import it.traveldreamClient.beans.dto.ProdottoAcquistabileDTO;
import it.traveldreamClient.beans.dto.UserDTO;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.security.RolesAllowed;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


import org.apache.commons.codec.digest.DigestUtils;

/**
 * Entity implementation class for Entity: Users
 *
 */
@Entity
@Table(name="USERS")

public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	@Column(nullable = false)
	private String Mail;
	@Column(nullable = false)
	private String Nome;
	@Column(nullable = false)
	private String Cognome;
	@Column(nullable = false)
	private String Password;
	
	
	
	@ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "USERS_GROUPS",
                    joinColumns = @JoinColumn(name = "email"))
    @Enumerated(EnumType.STRING)
    @Column(name="groupname")
	private List<Group> groups;
	
	@ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="GIFT_LISTS",
            joinColumns=
            @JoinColumn(name="mailProprietario", referencedColumnName="mail"),
      inverseJoinColumns=
            @JoinColumn(name="idComponente", referencedColumnName="idProdotto")
    )
	
    private List<Componente> listaRegali; 
	
	@ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="PURCHASES",
            joinColumns=
            @JoinColumn(name="mail", referencedColumnName="mail"),
      inverseJoinColumns=
            @JoinColumn(name="idProdotto", referencedColumnName="idProdotto")
    )
    private List<ProdottoAcquistabile> acquistiCompiuti; 
	
	
	@OneToMany
	private List<Pacchetto_Personal> preferiti;
	
	
	@PostConstruct
	public void init(){
		acquistiCompiuti=new LinkedList<ProdottoAcquistabile>();
		listaRegali=new LinkedList<Componente>();
	}
	
	
	@RolesAllowed({Group._USER})
	public List<Componente> getListaRegali() {
		return listaRegali;
	}

	@RolesAllowed({Group._USER})
	public void setListaRegali(List<Componente> listaRegali) {
		this.listaRegali = listaRegali;
	}


	public List<Group> getGroups() {
		return groups;
	}


	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	

	public List<ProdottoAcquistabile> getAcquistiCompiuti() {
		return acquistiCompiuti;
	}

	public void setAcquistiCompiuti(List<ProdottoAcquistabile> acquistiCompiuti) {
		this.acquistiCompiuti = acquistiCompiuti;
	}

	public List<Pacchetto_Personal> getPreferiti() {
		return preferiti;
	}

	public void setPreferiti(List<Pacchetto_Personal> preferiti) {
		this.preferiti = preferiti;
	}

	public User() {
		super();
	}   
	
	
	public User(UserDTO user){
		this.setCognome(user.getCognome());
		this.setNome(user.getNome());
		this.setMail(user.getEmail());
		this.Password =  DigestUtils.sha512Hex(user.getPassword() );		
	}
	
	
	public String getMail() {
		return this.Mail;
	}

	public void setMail(String mail) {
		this.Mail = mail;
	}   
	public String getNome() {
		return this.Nome;
	}

	public void setNome(String Nome) {
		this.Nome = Nome;
	}   
	public String getCognome() {
		return this.Cognome;
	}

	public void setCognome(String Cognome) {
		this.Cognome = Cognome;
	}   
	public String getPassword() {
		return this.Password;
	}

	public void setPassword(String Password) {
		this.Password = Password;
	}   
	
	public UserDTO getDTO() {
		UserDTO dto=new UserDTO();
		dto.setNome(Nome);
		dto.setCognome(Cognome);
		dto.setPassword(Password);
		dto.setEmail(Mail);
		List<ProdottoAcquistabileDTO> purchases=new LinkedList();
		for(ProdottoAcquistabile p : acquistiCompiuti){
			purchases.add(p.getDTO());
		}
		dto.setAcquisti(purchases);
		List<ComponenteDTO> listadoni=new LinkedList();
		for(Componente c: listaRegali){
			listadoni.add(c.getDTO());
		}
		dto.setGiftList(listadoni);
		
		return dto;
	}

   
}
